package com.applications.codemash.core;

import java.io.Serializable;
import java.text.DateFormat;
import java.util.Date;
import java.util.List;

import com.applications.codemash.utility.parser.SessionFeedParser;

import android.sax.Element;
import android.sax.EndTextElementListener;

public class Session implements Comparable<Session>, Serializable
{
	private static final long serialVersionUID = 1L;
	
	private String _uri;
	private String _title;
	private String _speakerName;
	private String _speakerUri;
	private Date _start;
	private String _abstract;
	private String _difficulty;
	private String _technology;
	private String _room;
	private String _track;
	private boolean _notify;
	private int _notificationId;
	
	public Session(Element sessionElement)
	{
		sessionElement.getChild(Constants.URI_ELEMENT_NAME).setEndTextElementListener(new EndTextElementListener() {
			@Override
			public void end(String body)
			{
				setUri(body);
			}
		});
		
		sessionElement.getChild(Constants.TITLE_ELEMENT_NAME).setEndTextElementListener(new EndTextElementListener() {
			@Override
			public void end(String body)
			{
				setTitle(body);
			}
		});
		
		sessionElement.getChild(Constants.ABSTRACT_ELEMENT_NAME).setEndTextElementListener(new EndTextElementListener() {
			@Override
			public void end(String body)
			{
				setAbstract(body);
			}
		});
		
		sessionElement.getChild(Constants.START_ELEMENT_NAME).setEndTextElementListener(new EndTextElementListener() {
			@Override
			public void end(String body)
			{
				try
				{
					body = body.replace("T", " ").replace("-", "/");
					Date dt = new Date(Date.parse(body));
					setStart(dt);
				}
				catch (IllegalArgumentException ex)
				{
					setStart(new Date());
				}
			}
		});
		
		sessionElement.getChild(Constants.ROOM_ELEMENT_NAME).setEndTextElementListener(new EndTextElementListener() {
			@Override
			public void end(String body)
			{
				setRoom(body);
			}
		});
		
		sessionElement.getChild(Constants.TRACK_ELEMENT_NAME).setEndTextElementListener(new EndTextElementListener() {
			@Override
			public void end(String body)
			{
				setTrack(body);
			}
		});
		
		sessionElement.getChild(Constants.DIFFICULTY_ELEMENT_NAME).setEndTextElementListener(new EndTextElementListener() {
			@Override
			public void end(String body)
			{
				setDifficulty(body);
			}
		});
		
		sessionElement.getChild(Constants.SPEAKER_NAME_ELEMENT_NAME).setEndTextElementListener(new EndTextElementListener() {
			@Override
			public void end(String body)
			{
				setSpeakerName(body);
			}
		});
		
		sessionElement.getChild(Constants.TECHNOLOGY_ELEMENT_NAME).setEndTextElementListener(new EndTextElementListener() {
			@Override
			public void end(String body)
			{
				// TODO Auto-generated method stub
				setTechnololgy(body);
			}
		});
		
		sessionElement.getChild(Constants.SPEAKER_URI_ELEMENT_NAME).setEndTextElementListener(new EndTextElementListener() {
			@Override
			public void end(String body)
			{
				setSpeakerUri(body);
			}
		});
	}
	
	private Session()
	{
		/* empty constructor used by copy */
	}
	
	/* Obligatory get/set methods (all Sets are protected) */
	public String getSessionUri() { return _uri; }
	public String getTitle() { return _title; }
	public String getSpeakerName() { return _speakerName; }
	public String getSpeakerUri() { return _speakerUri; }
	public Date getStart() { return _start; }
	public String getStartDisplay()
	{
		Date dt = getStart();
		DateFormat formatter = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT);
		return formatter.format(dt);
	}
	public Date getDay()
	{
		Date dt = getStart();
		Date newDate = new Date(dt.getYear(), dt.getMonth(), dt.getDay());
		
		return newDate;
	}
	
	public String getAbstract() { return _abstract; }
	public String getDifficulty() { return _difficulty; }
	public String getTechnology() { return _technology; }
	public String getTrack() { return _track; }
	public String getRoom()
	{
		if (_room == null || _room == "") return "No Room Determined";
		return _room;
	}
	public boolean getNotifyOnStart() { return _notify; }
	public int getNotificationId() { return _notificationId; }
	
	protected void setStart(Date date) { _start = date; }
	protected void setAbstract(String strAbstract) { _abstract = strAbstract; }
	protected void setDifficulty(String difficulty) { _difficulty = difficulty; }
	protected void setUri(String uri) { _uri = uri; }
	protected void setTitle(String title) { _title = title; }
	protected void setSpeakerName(String speakerName) { _speakerName = speakerName; }
	protected void setSpeakerUri(String speakerUri) { _speakerUri = speakerUri; }
	protected void setTechnololgy(String technology) { _technology = technology; }
	protected void setTrack(String track) { _track = track; }
	protected void setRoom(String room) { _room = room; }
	
	public void setNotifyOnStart(boolean notify) { _notify = notify; }
	public void setNotificationId(int notificationId) { _notificationId = notificationId; }
	
	public Session copy()
	{
		Session returnObject = new Session();
		returnObject.setAbstract(getAbstract());
		returnObject.setDifficulty(getDifficulty());
		returnObject.setSpeakerName(getSpeakerName());
		returnObject.setSpeakerUri(getSpeakerUri());
		returnObject.setStart(getStart());
		returnObject.setTitle(getTitle());
		returnObject.setUri(getSessionUri());
		returnObject.setTechnololgy(getTechnology());
		returnObject.setTrack(getTrack());
		returnObject.setRoom(getRoom());
		
		return returnObject;
	}
	
	/* static methods */
	public static List<Session> GetSessionList()
	{
		SessionFeedParser parser = new SessionFeedParser(Constants.GetAllSessionsUri());
		return parser.parse();
	}
	
	public static Session GetSessionForUri(String uri)
	{
		String fullUri = Constants.GetFullUri(uri);
		SessionFeedParser parser = new SessionFeedParser(fullUri);
		List<Session> result = parser.parse();
		
		if (result.size() > 0)
		{
			return result.get(0);
		}
		
		return null;
	}
	
	public static Session getNewSession()
	{
		Session session = new Session();
		return session;
	}
	
	@Override
	public String toString()
	{
		return getTitle();
	}

	@Override
	public int compareTo(Session another)
	{
		return getTitle().compareTo(another.getTitle());
	}
}